## R Beginner's Guide
## Chapter 4 - Collecting and Organizing Information
## by John M. Quick
## created June 7, 2010

## IMPORTING EXTERNAL DATA

#set the R working directory
#replace the sample location with one that is relevant to you
setwd("C://Novayre//GIT-Projects//book-Statistical Analysis With R//Chapter 04")

#copy the hanzhongResources.csv file into your working directory

#use read.csv(file) to read an external data file into R
#Shu resources located in Hanzhong, China
read.csv("..//data//hanzhongResources.csv")

## CREATING AND CALLING VARIABLES

#read the data from hanzhongResources.csv into a variable named hanzhongResources
hanzhongResources <- read.csv("..//data//hanzhongResources.csv")

#display the contents of the hanzhongResources variable
#Shu resources located in Hanzhong, China
hanzhongResources

#read the data from soldiersByCity.csv into a variable named soldiersByCity
soldiersByCity <- read.csv("..//data//soldiersByCity.csv")

#display the contents of soldiersByCity
soldiersByCity

## ACCESSING DATA WITHIN VARIABLES

#isolate a single column within a dataset using the variable$column notation.
#display the contents of the Soldiers column from the soldiersByCity variable
soldiersByCity$Soldiers

#isolate a single column within a dataset using the attach(variable) function and simplified notation
#attach the soldiersByCity variable
attach(soldiersByCity)
#display the contents of the Soldiers column from the soldiersByCity variable
Soldiers

#isolate a single row within a dataset using the variable[row, column] matrix notation.
#display the contents of the tenth row in the soldiersByCity variable
soldiersByCity[10,]

#isolate a single cell within a dataset using the variable[row, column] matrix notation.
#display the contents of cell [5,3] in the soldiersByCity variable
soldiersByCity[5,3]

## MANIPULATING VARIABLE DATA

#if a flood destroyed 75% of the Shu resources at Hanzhong, how much of each resource would remain?
#multiply the hanzhongResources variable by 0.25 to represent the remaining 25% of the original resources
hanzhongResources * 0.25

#if a flood destroyed 75% of the Provisions at Hanzhong, how much would remain?
#multiply the Provisions column by 0.25 to represent the remaining 25% of the original resources
hanzhongResources$Provisions * 0.25

#use the mean(data) function to calculate the average number of soldiers stationed in a Shu city
#on average, a Shu city has this many soldiers:
mean(soldiersByCity$Soldiers)

#save the mean number of soldiers per city into a new variable named meanSoldiersByCity
meanSoldiersByCity <- mean(soldiersByCity$Soldiers)
#display the contents of meanSoldiersByCity
meanSoldiersByCity

#what happens if we try to make a numeric calculation on nonnumeric data?
#we receive a warning, because it does not make sense to manipulate text mathematically
soldiersByCity * 5

#if a flood damaged half of our resources at Hanzhong, how much of each would remain?
hanzhongResourcesAfterFlood <- hanzhongResources / 2
#display the contents of hanzhongResourcesAfterFlood
hanzhongResourcesAfterFlood

#if 5000 soldiers were relocated from Guanghan to Baxi, how could this shift in resources be represented using R?
#add 5000 soldiers from the soldiersByCity cell that represents Baxi
baxiSoldiersAfterRelocation <- soldiersByCity[5,3] + 5000
#display the contents of baxiSoldiersAfterRelocation
baxiSoldiersAfterRelocation
#subtract 5000 soldiers from the soldiersByCity cell that represents Guanghan
guanghanSoldiersAfterRelocation <- soldiersByCity[6,3] - 5000
#display the contents of guanghanSoldiersAfterRelocation
guanghanSoldiersAfterRelocation

#Esta forma de hacerlo es mejor para no tener que mirar los datos
soldiersByCity[soldiersByCity$City == "Guanghan",3]-5000
soldiersByCity[soldiersByCity$City == "Baxi",3]+5000


#use min(data) and max(data) to calculate the minimum and maximum of a given dataset
#what is the mimimum number of soldiers in a Shu or Wei city?
minSoldiersByCity <- min(soldiersByCity[,3])
#display the contents of minSoldiersByCity
minSoldiersByCity
#what is the maximum number of soldiers in a Shu or Wei city?
maxSoldiersByCity <- max(soldiersByCity[,3])
#display the contents of maxSoldiersByCity
maxSoldiersByCity

#use sum(data) to calculate the sum a given dataset
#what is the total number of soldiers in the Shu and Wei armies?
totalSoldiers <- sum(soldiersByCity[,3])
#display the contents of totalSoldiers
totalSoldiers

## MANAGING THE R WORKSPACE

#save the R workspace to the working directory using save.image(file)
save.image("rBeginnersGuide_Ch_04.RData")

#load a previously saved R workspace using load(file)
load("rBeginnersGuide_Ch_04.RData")

#list the current contents of the R workspace using ls()
ls()

#quit R using the q() command
q()

## COLLECTING AND ORGANIZING NEW DATA

#read the contents of battleHistory.csv into a new variable named battleHistory
battleHistory <- read.csv("..//data//battleHistory.csv")

#what is the average number of soldiers to engage in combat for both the Shu and Wei forces?
meanSoldiersShu <- mean(battleHistory$ShuSoldiersEngaged)
#display the contents of meanSoldiersShu

#on average, the Wei army engages how many soldiers in battle?
meanSoldiersWei <- mean(battleHistory$WeiSoldiersEngaged)
#display the contents of meanSoldiersWei

#save your R workspace using save.image(file)
#remember to include the .RData file extension
save.image("rBeginnersGuide_Ch_04_hero.RData")

#save your R console text by copying and pasting it into a text file